package com.xaicode.learn.nio;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * file -> channel -> file.copy2
 *
 * @author Locker xaicode@sina.com
 * @version 1.0
 * @date 2021/6/13 0013 1:36
 */
@Slf4j
public class NIOFileChannel04 {

    private static final String FILE_PATH = "logs" + File.separator;
    private static final String FILE_IN = FILE_PATH + "NIOFileChannel01.txt";
    private static final String FILE_OUT = FILE_PATH + "NIOFileChannel01.txt.copy2";

    public static void main(String[] args) {

        try (
                // create file input stream
                FileInputStream inputStream = new FileInputStream(FILE_IN);
                // create file output stream
                FileOutputStream outputStream = new FileOutputStream(FILE_OUT);
                // channel
                FileChannel inputFileChannel = inputStream.getChannel();
                FileChannel outputFileChannel = outputStream.getChannel();
        ) {

            outputFileChannel.transferFrom(inputFileChannel, 0, inputFileChannel.size());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
